Date.prototype.format = function(format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(format))
            format = format.replace(RegExp.$1,
                RegExp.$1.length == 1 ? o[k] :
                ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}   

$.urlParam = function(name) {
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (results == null) {   
        return null;
    } else {
        return results[1] || 0;
    }
}

var buildId = $.urlParam('buildId');
var time = $.urlParam('time');
time = time == null ? 5 : time;

// 建筑电
var buildEle = 'ca6e8979-65b0-4d9d-8693-1e0dab158e05,79fe9945-b79f-4f07-b9f1-2bb4bd48db52,756b1e3d-e72d-45f0-a263-402855a11876,0b6db80c-af44-4655-88a8-80d7d5b78102';

// 建筑水
var buildWater = '41c7e16d-bf8d-4467-a7da-583045cc33e8';
// 部门id
var deptIds = '3f972eb9-157c-4806-8c7a-54083cf26af9,e8a85ee6-c07b-4898-9bf3-dc834115fdb9,91d742cf-08b4-4e3a-9b07-81e585826734,0734a3a6-9f0f-48f7-8b0b-6159b396ba64,705fe3ef-5bbe-4915-9f50-09e1e40af050,34c6fd0d-04e5-4173-acc6-c8ffc51353f7';

var eiIde = '7aef64fc-d6b5-11e4-bba2-000c2969e1e1'; //总电分项id
var eiIdw = '7af48e3b-d6b5-11e4-bba2-000c2969e1e1'; //总水分项id

var deptIdAll = '61dc482f-60c4-403f-8010-eb9a115ce8ba'; //部门根节点
var buildIdAll = 'e8ac3a0d-6ee6-48dd-8ae4-d1166b0308c7'; //建筑根节点

$(document).ready(function() {
    var $_doc = $(document);

    var screenHeight = document.documentElement.clientHeight;
    var autoHeight = screenHeight;

    $('#autoTab1 tr').each(function(i, n) {
        var $n = $(n);
        var id = $n.attr('id');
        if (id && id == 'autoTabTr1') {} else {
            autoHeight -= $n.height();
        }
    });
    var titleHeight = $('#nowEnergyRealTimeTitlt').height();
    $('#main1').css('height', (autoHeight - titleHeight) + 'px');


    function rdFloat(n, m) {
        var result;
        for (var i = 0; i < 100; i++) {
            var val = Math.random();
            if (n <= val && val <= m) {
                result = val;
            }
        }
        return result;
    }

    var isTrueRead = false;

    function setCompareInfo(yesterVal, nowVal, $compare, $diff, $nowVal, $percent) {
        // var compareId = $compare.attr('id');
        // if (compareId == 'eleCompare') {
        //     var badEleData = parseFloat($_doc.data('badEleData'));
        //     if (nowVal <= badEleData) {
        //         badEleData = badEleData + rdFloat(0.1, 0.2);
        //         nowVal = badEleData.toFixed(2);
        //         $_doc.data('badEleData', nowVal);
        //         $_doc.data('yesterVal', yesterVal);
        //     } else {
        //         $_doc.data('badEleData', nowVal);
        //         $_doc.data('yesterVal', yesterVal);
        //     }
        // }


        $nowVal.text(nowVal);
        var diffVal = (nowVal - yesterVal).toFixed(2);
        var percent;
        if (yesterVal == 0) {
            percent = 100;
        } else {
            percent = ((diffVal / yesterVal) * 100).toFixed(2);
        }

        $nowVal.removeClass('data_up').removeClass('data_down');

        if (diffVal > 0) {
            $compare.html('涨');
            $nowVal.addClass('data_up');
            $percent.text('+' + percent + '%');
        } else if (diffVal == 0) {
            $compare.html('持平');
        } else if (diffVal < 0) {
            $compare.html('低');
            diffVal = -diffVal;
            $nowVal.addClass('data_down');
            $percent.text('-' + (-percent) + '%');
        }
        if (diffVal != 0) {
            $diff.text(diffVal);
        } else {
            $diff.text('');
        }
    }

    function doSome(data) {
        var yesterEleTotalVal = data.yesterEleTotalVal;
        var nowEleTotalVal = data.nowEleTotalVal;
        var yesterWaterTotalVal = data.yesterWaterTotalVal;
        var nowWaterTotalVal = data.nowWaterTotalVal;

        var yesterAvgTem = data.yesterAvgTemp;
        var nowAvgTemp = data.nowAvgTemp;

        var yesterIsWork = data.yesterIsWork;
        var nowIsWork = data.nowIsWork;

        setCompareInfo(yesterEleTotalVal, nowEleTotalVal, $('#eleCompare'), $('#eleDiff'), $('#nowEleVal'), $('#elePercent'));

        setCompareInfo(yesterWaterTotalVal, nowWaterTotalVal, $('#waterCompare'), $('#waterDiff'), $('#nowWaterVal'), $('#waterPercent'));
    }

    function pullEnergyCompareInfo(buildId) {

        $.get('adminScreen.do?action=ShowElectricWaterCompareAction', {
            buildId: buildId,
            event_submit_do_show: 'method',
            model: 'hour'
        }, doSome);
    }

    setInterval(function() {
        // var nowData = new Date();
        // var mins = nowData.getMinutes();
        // var num = mins % 10;
        // if (num == 1 && isTrueRead) {
        //     pullEnergyCompareInfo(buildId);
        //     isTrueRead = false;
        // } else if (num == 1) {
        //     setCompareInfo($_doc.data('yesterVal'), parseFloat($_doc.data('badEleData')), $('#eleCompare'), $('#eleDiff'), $('#nowEleVal'), $('#elePercent'));
        // } else {
        //     isTrueRead = true;
        //     setCompareInfo($_doc.data('yesterVal'), parseFloat($_doc.data('badEleData')), $('#eleCompare'), $('#eleDiff'), $('#nowEleVal'), $('#elePercent'));
        // }
        pullEnergyCompareInfo(buildId);
    }, 1000 * 5);
    pullEnergyCompareInfo(buildId);

})
document.write("<script src='assets/js/ledpm_lsk.js' type='text/javascript'></script>");
document.write("<script src='assets/js/ledpm_sl.js' type='text/javascript'></script>");